 //===================1.显示表格============================== 
$(function(){
	var editRow=undefined;
	$('#datagrid1').datagrid({    
    url:'showproduct.action',  
	 
	 method:'get',
     idField:'productId' ,
     singleSelect:true,
     fitColumns:true,
     
      
       columns:[[
           {title:'产品编码',
            field:'productId',
            width:100,
            },
           {title:'类别编码',
             field:'pclassId',
            width:100,
            editor:{
            	type:'validatebox',
            	options:{required:true}
            }  
            },
           {title:'产品名称',
                field:'productName',
                width:100,
                editor:{
                	type:'validatebox',
                	options:{required:true}
                }  
                
            },
            {title:'产品库存',
                    field:'pstock',
                    width:100,
                    editor:{
                    	type:'validatebox',                   	
                    	options:{
                    		required:true
                    		
                    		}
                    }  
             },
            {title:'产品类别2222',
                  field:'pclassName',
                  width:100,
                  editor:{
                	  type:'combobox',
                  	options:{required:true,
                  		 url:'pclass_showpclasscombobox.action',    
             		    valueField:'pclassName',    
             		    textField:'pclassName'   }
                  }  
             },  
             {title:'产品条形码',
                 field:'pbarcode',
                 width:100,
                 editor:{
                	 type:'validatebox',
                 	options:{required:true}
                 }  
             },
             {title:'产品价格',
                 field:'pprice',
                 width:100,
                 editor:{
                	 type:'validatebox',
                 	options:{required:true}
                 }  
             }
            ] ] ,   
    iconCls:'icon-load' ,
    pageSize:10,
    pageList:[3,6,10,15],
    border:false,
    pagination:true,
    sortOrder:'desc',
    sortName:'name',
    editable:true,
    //=========================2.1添加按钮一栏=========================================
    toolbar:[
             {text:'添加',
              iconCls:'icon-add',
              handler: function(){
            	  if(editRow!=undefined){
            		  $('#datagrid1').datagrid("endEdit",editRow);
            		  editRow=undefined;
            		}
            	  
            	  if(editRow==undefined){
            		  $('#datagrid1').datagrid('insertRow',{
   	               index: 0,	// 索引从0开始
	                 row: {
	                	 //  productId:'',
	                	   pclassId:'',
	                		productName:'',
	                		 pstock:'',
	                		pclassName:'',
	                		pbarcode:'',
	                		pprice:''} 
	                		
                  });
                       $('#datagrid1').datagrid('beginEdit',0);
                              editRow=0;}
            	  
              
                
              
              }
             },'-',
//=========================2.2删除按钮=================================
             {text:'删除',
              iconCls:'icon-remove',
 //===================================================================            
            handler: function(){
            	  
            	  remove();
            	  
              },
 
             },'-',
             {text:'编辑',
             
              iconCls:'icon-edit',
//===============================2.3编辑按钮======================================================
              handler: function(){
            	  var rows;
            	  rows=$('#datagrid1').datagrid('getSelections');
            	 if(rows.length!=1){
            		
            		 $.messager.alert('提示','魏沁,你还没有选择行','error');
            	 };
            	 if(rows.length==1){
            		 if(editRow!=undefined){
            		 $('#datagrid1').datagrid('endEdit',editRow);
            		editRow=undefined;
            	};
            		 if(editRow==undefined){
            			 var index=$('#datagrid1').datagrid('getRowIndex',rows[0]);
                		 $('#datagrid1').datagrid('beginEdit',index);
                		 editRow=index};
            	 }
            	  
            	  
            	  
            	  
              },
             },'-',
 //======================================2.4保存按钮=============================================
             {text:'保存',
                 iconCls:'icon-remove',
                 handler: function(){
               $('#datagrid1').datagrid('endEdit', editRow);
                 editRow=undefined;
                $('#datagrid1').datagrid('unselectAll');//我自己添加的
                 }
                },'-',
//======================================2.4保存按钮=============================================
             {text:'取消',
                 iconCls:'icon-remove',
                 handler: function(){
              
                 editRow=undefined;
                 $('#datagrid1').datagrid('rejectChanges');
                 $('#datagrid1').datagrid('unselectAll');
                 
                 }
                }
            ],
//===============================3.数据提交===========================
            onAfterEdit:function(rowIndex,rowData,changes){
            	var inserted=$('#datagrid1').datagrid("getChanges","inserted");
            	var updated=$('#datagrid1').datagrid("getChanges","updated");
            	var url='';
            	if(inserted.length>0){
            		$.post('addproduct.action', 
            				rowData,
                             function (result) { 
                                if (result.success) {
                                	
                                   $('#datagrid1').datagrid('load');
                                    $.messager.show({   //显示正确信息
                                        title: '提示',
                                        msg: result.msg
                                    });                                 
                                } else {
                                    $.messager.show({   //显示错误信息
                                        title: '错误',
                                        msg: result.msg
                                    });
                                }
                            }, 'json');
            	}
                  if(updated.length>0){
                	  $.post('editproduct.action', 
                              rowData,
                               function (result) { 
                                  if (result.success) {
                                 
                                     $('#datagrid1').datagrid('load');
                                      $.messager.show({   //显示正确信息
                                          title: '提示',
                                          msg: result.msg
                                      });                                 
                                  } else {
                                      $.messager.show({   //显示错误信息
                                          title: '错误',
                                          msg: result.msg
                                      });
                                  }
                              }, 'json');
            	}
                  
            	
            	
            	editRow=undefined;
            },
  
            
          
})




});


//===============================6编辑方法===================================================/* 删除数据*/
function remove(){
    var row = $('#datagrid1').datagrid('getSelected');
    //如果没有选中记录
    if(row == null){
        $.messager.alert("提示", "请选择一条记录",'info');
        return;
    }
    $.messager.confirm('确认', '确定要删除吗？', function (r) {
            if (r) {
                //提交到后台的action
                $.post('delproduct.action', 
                { productId: row.productId },
                 function (result) { 
                    if (result.success) {
                    	
                    	
                        $.messager.show({   //显示正确信息
                            title: '提示',
                            msg: result.msg
                        });      
                        $('#datagrid1').datagrid('reload');
                    } else {
                        $.messager.show({   //显示错误信息
                            title: '错误',
                            msg: result.msg
                        });
                    }
                }, 'json');
            }
        });
     
}
//======================1.查询功能=========================================================	


beginsearch=function(){
	$('#datagrid1').datagrid('load',{productId:$('#form1').find('[name=productId]').val(),
		productName:$('#form1').find('[name=productName]').val(),
		pclassName:$('#form1').find('[name=pclassName]').val(),
		ppriceLow:$('#form1').find('[name=ppriceLow]').val(),
		ppriceUp:$('#form1').find('[name=ppriceUp]').val(),});

	
};
//======================2.清空查询========================================================

clearsearch=function(){
	$('#form1').find('input').val('');
	
	$('#datagrid1').datagrid('load',{});

	
};

